// 创建管理用户信息的状态机切片【子模块】
import { UserInfoType } from "@/types/user";
import { createSlice } from "@reduxjs/toolkit";
import store2 from "store2";
interface UserStateType {
  userInfo: UserInfoType | null;
}
let initialState: UserStateType = {
  userInfo: null,
};
// 尝试提取本地存储
let uinfo = store2.get("react-admin-2302");
if (uinfo) {
  initialState.userInfo = uinfo;
}
const userSlice = createSlice({
  name: "user",
  initialState,
  //此处的reducers内部配置的函数，有两个作用action、reducer
  reducers: {
    initUserInfo(state, action) {
      state.userInfo = action.payload;
    },
  },
});

export const { initUserInfo } = userSlice.actions; //对外抛出action
export default userSlice.reducer; //对外抛出reducer
